package com.gmall.web.controller;

import com.gmall.client.feign.ProductFeignClient;
import com.gmall.common.result.Result;
import com.gmall.model.product.SkuInfo;
import com.gmall.web.utils.FreemarkerUtil;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("item")
public class ItemController {


    @Autowired
    ProductFeignClient productFeignClient;

    @GetMapping("genItemHTML/{skuId}")
    public String genItemHTML(@PathVariable Long skuId) throws IOException, TemplateException {
        Map map_data=new HashMap();
        Result<SkuInfo> result = productFeignClient.getSkuInfoById(skuId);
        if(result.isOk()){
            map_data.put("skuInfo",result.getData() );
        }

        FreemarkerUtil freemarkerUtil=new FreemarkerUtil();
        //获取部署文件的resource目录
        String classpath = this.getClass().getResource("/").getPath();

        freemarkerUtil.createHtml(map_data,"item",
                classpath + "/static/item_" + skuId +".html");


        return "OK";
    }
}
